package com.fho4565.commands;

import com.fho4565.main.Utils;
import com.fho4565.packet.PlanCommandMessagePacket;
import com.fho4565.packet.PlanCommandPacketHandler;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.world.InteractionHand;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.eventbus.api.Event;
//TODO 1.3.0版本完成
public class PlanCommand {
    static String command = "";

    public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
        dispatcher.register(
                Commands.literal("plan").requires(s -> s.hasPermission(2))
                        .executes(
                                context -> {
                                    Utils.sendCdFeedback(context, command);
                                    return 1;
                                }
                        )
                        .then(Commands.argument("command", StringArgumentType.string()).executes(
                                context -> {
                                    command = StringArgumentType.getString(context, "command");
                                    return 1;
                                }
                        ))

        );
    }

    public static void playerRightClick(PlayerInteractEvent.RightClickEmpty event) {
        if (event.getHand() == InteractionHand.OFF_HAND) {
            if (command.equals("")) {
                return;
            }
            PlanCommandPacketHandler.sendToServer(new PlanCommandMessagePacket(command));
            event.setResult(Event.Result.DENY);
        }
    }
    public static void playerRightClickB(PlayerInteractEvent.RightClickBlock event) {
        if (event.getSide().isServer()){return;}
        if (event.getHand() == InteractionHand.OFF_HAND) {
            if (command.equals("")) {
                return;
            }
            PlanCommandPacketHandler.sendToServer(new PlanCommandMessagePacket(command));
            event.setResult(Event.Result.DENY);
        }
    }
}
